/**
 * Created by coeus on 17/3/19.
 */

class ObjectUtil {

    static isEmpty(val) {

        if (val == null) {
            return true;
        }

        switch (typeof(val)) {
            case 'string':
                return ObjectUtil.trim(val).length == 0 ? true : false;
            case 'number':
                return val == 0;
            case 'object':
                if (val.constructor == Array) {
                    return val.length == 0;
                } else if (val.constructor == Object) {
                    if (val == null) {
                        return true;
                    } else {
                        for (let name in val) {
                            return false;
                        }
                        return true;
                    }
                } else {
                    return val == null;
                }
            case 'array':
                return val.length == 0;
            case 'boolean':
                return false;
            case 'function':
                return false;
            default:
                return true;
        }
    }

    /**
     * 去除空格
     * @param text
     * @returns {*}
     */
    static trim(text) {
        if (typeof(text) == "string") {
            return text.replace(/^\s*|\s*$/g, "");
        } else {
            return text;
        }
    }

    /**
     * 处理treeNode参数
     */
    static getTreeData(treeList = []) {
      return treeList.map(item => {
        if(item.children && item.children.length !== 0) {
          this.getTreeData(item.children);
        } else {
          item.children = null;
        }
        item.label = item.name;
        item.value = item.id;
        return item;
      })
    }
}

export default ObjectUtil;
export {ObjectUtil};